var userModel = require("./usermodel");
//把表拿来了
var user = {
    list: function() {
        return new Promise(function(resolve, reject) {
            userModel.find(function(err, data) {
                if (!err) {
                    resolve(data);
                } else {
                    reject(err);
                }
            })
        });
    },
    add: function(userinfo) {
        //userModel.insertMany([userinfo])
        var that = this;
        return new Promise(function(resolve, reject) {
            var result = {};
            that.findUserByUsername(userinfo.username).then(function(flag) {
                // console.log(flag);
                if (flag) { //告诉用户名存在
                    result.code = 0;
                    result.msg = "该用户名已经存在";
                    resolve(result);
                } else {
                    //执行新增
                    userModel.insertMany([userinfo], (err, data) => {
                        if (!err) {
                            if (data.length > 0) {
                                result.code = 1;
                                result.msg = "新增成功";
                                result.id = data[0]["_id"];
                            } else {
                                result.code = 0;
                                result.msg = "新增失败";
                            }
                        } else {
                            result.code = 0;
                            result.msg = "网络异常";
                        }
                        resolve(result);
                    })

                }

            }, function() {

            })

        });


    },
    findUserByUsername: function(username) {
        return new Promise(function(resolve, reject) {
            userModel.findOne({
                username: username
            }, function(err, data) {
                if (!err) {
                    if (data) {
                        resolve(true); //表示用户存在
                    } else {
                        resolve(false); //表示用户不存在
                    }
                } else {
                    console.log(err);
                    //利用fs.writeFile 就错误写入到log.txt中
                    reject(err);
                }
            })

        });
    },
    login: function(username, userpwd) {
        return new Promise(function(resolve, reject) {
            var result = {};
            userModel.findOne({
                username: username
            }, (err, data) => {
                if (!err) {
                    if (data) {
                        if (data.userpwd == userpwd) {
                            result.code = 1;
                            result.msg = "登录成功";
                            result.id = data["_id"];
                            result.usertype = data.usertype;
                        } else {
                            result.code = 0;
                            result.msg = "用户名和密码不匹配";
                        }
                    } else {
                        result.code = 0;
                        result.msg = "用户名不存在";
                    }

                    resolve(result);

                } else {
                    // reject(err);
                    console.log(err);
                    result.code = 0;
                    result.msg = "网络异常,服务器正在维护";
                    reject(result);
                }
            })
        });
    },
    deletebyid: function(id) {
        return new Promise((resolve, reject) => {
            var result = new Object();
            userModel.findByIdAndDelete({
                _id: id
            }, (err, res) => {
                if (!err) {
                    // console.log(res);
                    result.code = 1;
                    result.msg = "删除成功";
                } else {
                    result.code = 0;
                    result.msg = "删除失败"
                }
                resolve(result);
            })
        })

    }
}
module.exports = user;